<template>
  <div>
    <TopFilter :list="topFilterList" :cols="3" :collapse="false" :isSubmitBtn="false"></TopFilter>
    <FilterTable ref="table" columnsRef="ygy_workerHistory" :isSelectColumn="false" :columns="columns" :dataSource="tableData" :onColumnsChange="columns => (this.columns = columns)"></FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import dictCode from '@/utils/dictCode';
import { getAllRepairAssign } from '@/api/repair/repairManage/repairQuery';
import { getNameByIds } from '@/api/repair/common/index.js';

export default {
  name: 'workerHistory',
  mixins: [dictionary],
  props: ['rowData', 'allUser'],
  data() {
    return {
      topFilterList: this.createTopFilter(),
      columns: this.createTableColumns(),
      tableData: []
    };
  },

  mounted() {
    this.queryWorkerLog();
    this.topFilterList[0].initialValue = this.rowData.labourName;
    this.topFilterList[1].initialValue = this.rowData.assignLabourHour;
    this.topFilterList[2].initialValue = this.rowData.remark;
  },

  methods: {
    // 获取所有的人员
    async getNameByIds(parmas) {
      let res = await getNameByIds(parmas);
      if (res.resultCode == 200) {
        let userList = res.data.map(x => ({ text: x.employeeName, value: x.employeeId + '' }));
        this.columns.find(x => x.dataIndex === 'technician').dictItems = userList;
      }
    },
    dealerTranslate() {
      let userIds = [];
      this.tableData.forEach(item => {
        userIds.push(item.technician);
      });
      if (!userIds.length) return;
      let newUserIds = Array.from(new Set(userIds)); // 数组去重
      let userIdsStr = newUserIds.join(',');
      if (!userIdsStr.length) return;
      this.getNameByIds({ userIds: userIdsStr });
    },
    createTopFilter() {
      return [
        {
          type: 'INPUT',
          label: '项目名称',
          fieldName: 'labourName',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '派工工时',
          fieldName: 'assignLabourHour',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '检查结果',
          fieldName: 'remark',
          disabled: true
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '工位',
          dataIndex: 'labourPositionName',
          sorter: true
        },
        {
          title: '责任技师',
          dataIndex: 'technician',
          sorter: true,
          dictItems: []
        },
        {
          title: '派工工时',
          dataIndex: 'assignLabourHour',
          sorter: true,
          precision: 2
        },
        {
          title: '开工时间',
          dataIndex: 'itemStartTime',
          sorter: true,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          width: 150
        },
        {
          title: '完工时间',
          dataIndex: 'itemEndTime',
          sorter: true,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          width: 150
        }
      ];
    },

    // 派工情况查询
    async queryWorkerLog() {
      const itemId = this.rowData.itemId;
      const res = await getAllRepairAssign({ itemId: itemId, ownerCode: this.rowData.ownerCode });
      console.log(res, 'res');
      this.tableData = res.data.rows;
      this.dealerTranslate();
    },
    closeDialog() {
      this.$emit('close');
    }
  }
};
</script>

<style lang="less"></style>
